<template>
  <div class="list-table">
    <div class="area-header">
      <span class="area-header-title">{{ $t('投资回款') }}</span>
    </div>
    <el-form ref="queryForm" :model="queryParams" :inline="true">
      <el-form-item :label="$t('申请标题')" prop="actTitle">
        <el-input v-model="queryParams.actTitle" :placeholder="$t('申请标题')" size="small" />
      </el-form-item>
      <el-form-item :label="$t('回款类型')" prop="distributionType">
        <el-select v-model="queryParams.distributionType" :placeholder="$t('回款类型')" clearable>
          <el-option
            v-for="dict in distributionTypeOptions"
            :key="dict.dictValue"
            :label="$t(dict.dictLabel)"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('权益变更时间')" prop="distributionDate">
        <DatePicker v-model="distributionDates" type="daterange" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="queryHandle">{{ $t("搜索") }}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t("重置") }}</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['deal:distribution:edit']"
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="addHandle"
        >{{ $t("新增") }}</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['deal:distribution:export']"
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="exportHandle"
        >{{ $t("导出") }}</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" tooltip-effect="darkTable" :data="dealDistributionList" :show-summary="true" :summary-method="getSummaries" border>
      <el-table-column :label="$t('序号')" type="index" align="center" show-overflow-tooltip width="50">
        <template slot-scope="scope">
          {{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('申请标题')" align="center" prop="actTitle" :show-overflow-tooltip="true" width="250">
        <template slot-scope="scope">
          <span class="link-type" @click="updateHandle(scope.row)">{{ scope.row.actTitle }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('回款类型')" align="center" prop="distributionType" :formatter="distributionTypeFormat" />
      <el-table-column :label="$t('权益变更时间')" align="center" prop="distributionDate" width="120">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.distributionDate) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('预计回款总额') + $t('金额单位')" header-align="center" align="right" prop="amountDistribution" width="130">
        <template slot-scope="scope">
          <span class="money-text">{{ moneyFormat(scope.row.amountDistribution, 'w') }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('实际回款总额') + $t('金额单位')" header-align="center" align="right" prop="totalAmountOfActualCollection" width="130">
        <template slot-scope="scope">
          <span class="money-text">{{ moneyFormat(scope.row.totalAmountOfActualCollection, 'w') }}</span>
        </template>
      </el-table-column>

      <el-table-column :label="$t('回款后持股比例(%)')" align="center" prop="exitShareRatio" width="150" />
      <el-table-column :label="$t('币种')" align="center" prop="currency" :formatter="currencyFormat" />
      <!--
      <el-table-column :label="$t('本金') + $t('金额单位')" align="center" prop="amountPrincipal">
        <template slot-scope="scope">
          <span class="money-text">{{ moneyFormat(scope.row.amountPrincipal, 'w') }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('收益') + $t('金额单位')" align="center" prop="amountIncome" width="100">
        <template slot-scope="scope">
          <span class="money-text">{{ moneyFormat(scope.row.amountIncome, 'w') }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('分红') + $t('金额单位')" align="center" prop="amountBonus" width="100">
        <template slot-scope="scope">
          <span class="money-text">{{ moneyFormat(scope.row.amountBonus, 'w') }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('其它') + $t('金额单位')" align="center" prop="amountOthers" width="100">
        <template slot-scope="scope">
          <span class="money-text">{{ moneyFormat(scope.row.amountOthers, 'w') }}</span>
        </template>
      </el-table-column>
      -->
      <el-table-column :label="$t('审批状态')" fixed="right" align="center" width="120">
        <template slot-scope="scope">
          <ProcessStatus :status="scope.row.actStatus" :act-id="scope.row.actId" />
        </template>
      </el-table-column>
      <el-table-column :label="$t('操作')" fixed="right" align="center" class-name="small-padding fixed-width" width="150">
        <template slot-scope="scope">
          <!-- 启动流程，终止流程，修改，删除 四个按钮 -->
          <ListBtn :bus-data="scope.row" has-permi="deal:distribution" />
          <el-button
            v-if="scope.row.actStatus === 'W'"
            size="small"
            type="text"
            icon="el-icon-edit"
            @click="updateTimeMoney(scope.row)"
          >
            {{ $t('维护回款时间') }}
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改投资回款对话框 -->
    <el-dialog :title="$t(title)" :visible.sync="formOpen" :close-on-click-modal="false" :lock-scroll="false" custom-class="dialogForm" append-to-body>
      <dealDistributionForm ref="dealDistributionForm" />
    </el-dialog>

    <el-dialog :title="$t(title)" :visible.sync="formPaymentOpen" :close-on-click-modal="false" :lock-scroll="false" custom-class="dialogForm" :destroy-on-close="true" append-to-body>
      <dealDistributionPayment ref="dealDistributionPayment" />
    </el-dialog>

  </div>
</template>

<script>
import {
  listDealDistribution,
  delDealDistribution,
  getDealDistributionSum
} from '@/api/deal/distribution/dealDistribution'
import dealDistributionForm from './dealDistributionForm.vue'
import ProcessStatus from '@/components/Act/ProcessStatus/index'
import dealDistributionPayment from '@/views/deal/distribution/dealDistributionPayment.vue'
export default {
    components: {
      dealDistributionForm,
      ProcessStatus,
      dealDistributionPayment
    },
  data() {
      return {
          // 遮罩层
          loading: true,
          // 总条数
          total: 0,
          // 投资回款表格数据
          dealDistributionList: [],
          dealDistributionSum: {},
          // 弹出层标题
          title: '投资回款',
          // 是否显示弹出层
          formOpen: false,
        formPaymentOpen: false,
        // 回款类型字典
        distributionTypeOptions: [],
        // 币种字典
        currencyOptions: [],
        // 关联退出字典
        joinExitOptions: [],
        // 查询条件 回款时间
        distributionDates: [],
        // 查询参数
        queryParams: {
            actTitle: undefined,
            dealId: this.$route.query.id,
            distributionType: undefined,
            startDate: undefined,
            endDate: undefined,
            pageNum: 1,
            pageSize: 10
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
        distributionType: [
          { required: true, message: this.$t('必填信息'), trigger: 'blur' }
        ], distributionDate: [
          { required: true, message: this.$t('必填信息'), trigger: 'blur' }
        ], amountPrincipal: [
          { required: true, message: this.$t('必填信息'), trigger: 'blur' }
        ], amountIncome: [
          { required: true, message: this.$t('必填信息'), trigger: 'blur' }
        ], amountBonus: [
          { required: true, message: this.$t('必填信息'), trigger: 'blur' }
        ], amountOthers: [
          { required: true, message: this.$t('必填信息'), trigger: 'blur' }
        ], amountDistribution: [
          { required: true, message: this.$t('必填信息'), trigger: 'blur' }
        ], currency: [
          { required: true, message: this.$t('必填信息'), trigger: 'blur' }
        ], joinExit: [
          { required: true, message: this.$t('必填信息'), trigger: 'blur' }
        ], exitShareRatio: [
          { required: true, message: this.$t('必填信息'), trigger: 'blur' }
        ] }
      }
  },
  created() {
      this.getList()
      this.getDicts('distribution_mode').then(response => {
          this.distributionTypeOptions = response.data
      })
      this.getDicts('currency').then(response => {
          this.currencyOptions = response.data
      })
      this.getDicts('exit_mode').then(response => {
          this.joinExitOptions = response.data
      })
  },
  methods: {
    /** 查询投资回款列表 */
    getList() {
        this.loading = true
        if (this.distributionDates != null && this.distributionDates.length > 1) {
          this.queryParams.startDate = this.distributionDates[0]
          this.queryParams.endDate = this.distributionDates[1]
        } else {
          this.queryParams.startDate = undefined
          this.queryParams.endDate = undefined
        }
        listDealDistribution(this.queryParams).then(response => {
          getDealDistributionSum(this.queryParams).then(res => {
            this.dealDistributionSum = res.data
          })
            this.dealDistributionList = response.rows
            this.total = response.total
            this.loading = false
        })
    },
    // 回款类型字典翻译
    // eslint-disable-next-line no-unused-vars
    distributionTypeFormat(row, column) {
      return this.selectDictLabel(this.distributionTypeOptions, row.distributionType)
    },
    // 币种字典翻译
    // eslint-disable-next-line no-unused-vars
    currencyFormat(row, column) {
      return this.selectDictLabel(this.currencyOptions, row.currency)
    },
    // 关联退出字典翻译
    // eslint-disable-next-line no-unused-vars
    joinExitFormat(row, column) {
      return this.selectDictLabel(this.joinExitOptions, row.joinExit)
    },

    // 表单重置
    reset() {
        this.form = {
            id: undefined,
            dealId: undefined,
            distributionType: undefined,
            distributionDate: undefined,
            amountPrincipal: undefined,
            amountIncome: undefined,
            amountBonus: undefined,
            amountOthers: undefined,
            amountDistribution: undefined,
            currency: undefined,
            createBy: undefined,
            createTime: undefined,
            updateBy: undefined,
            updateTime: undefined,
            remark: undefined,
            delFlag: undefined,
            joinExit: undefined,
            exitShareRatio: undefined
        }
        this.resetForm('form')
    },
    /** 搜索按钮操作 */
    queryHandle() {
        this.queryParams.pageNum = 1
        this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
        this.resetForm('queryForm')
        this.distributionDates = []
        this.queryHandle()
    },
    /** 新增按钮操作 */
    addHandle() {
        this.formOpen = true
        this.$nextTick(() => {
            this.$refs.dealDistributionForm.init(null)
        })
    },
    /** 修改按钮操作 */
    updateHandle(row) {
        this.formOpen = true
        this.$nextTick(() => {
            // 调用form页面的 init 方法初始化数据
            this.$refs.dealDistributionForm.init(row)
        })
    },
    /** 删除按钮操作 */
    deleteHandle(row) {
        this.$confirm(this.$t('是否确认删除?'), this.$t('警告'), {
            confirmButtonText: this.$t('确定'),
            cancelButtonText: this.$t('取消'),
            type: 'warning'
        }).then(function() {
            return delDealDistribution(row.id)
        }).then(() => {
            this.getList()
            this.msgSuccess(this.$t('删除成功'))
        }).catch(function() {
        })
    },
    /** 导出按钮操作 */
    exportHandle() {
        this.downLoadExcel('/distribution/dealDistribution/export', this.queryParams)
    },
    updateTimeMoney(row) {
      this.formPaymentOpen = true
      this.$nextTick(() => {
        this.$refs.dealDistributionPayment.actTitle = row.actTitle
        this.$refs.dealDistributionPayment.init(row)
      })
    },
    // 合计
    getSummaries(param) {
      const { columns } = param
      const sums = []
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = '合计'
        } else if (index === 4) {
          sums[index] = this.moneyFormat(this.dealDistributionSum.amountDistribution, 'w')
        } else if (index === 5) {
          sums[index] = this.moneyFormat(this.dealDistributionSum.totalAmountOfActualCollection, 'w')
        } else {
          sums[index] = ''
        }
      })
      return sums
    }
  }
}
</script>
